package by.parfen.game.service.coder.impl;

import java.io.StringReader;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;

import by.parfen.game.datamodel.GamePlayer;
import by.parfen.game.service.coder.GamePlayerCoder;

public class GamePlayerCoderImpl implements GamePlayerCoder {

	@Override
	public JsonObject encode(GamePlayer gamePlayer) {
		JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
		jsonBuilder.add("name", gamePlayer.getName());
		JsonObject jsonObject = jsonBuilder.build();
		return jsonObject;
	}

	@Override
	public GamePlayer decode(JsonObject jsonGamePlayer) {
		GamePlayer gamePlayer = new GamePlayer();
		JsonObject jsonObject = Json.createReader(new StringReader(jsonGamePlayer.toString())).readObject();
		gamePlayer.setName(jsonObject.getString("name"));
		return gamePlayer;
	}

}
